package com.zzyl.nursing.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.dto.NursingProjectDto;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.service.INursingProjectService;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 护理项目Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        //1.插入数据
        int result = nursingProjectMapper.insert(nursingProject);

        //2.删除缓存
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);

        //3.返回结果
        return result;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        //1.修改数据
        int result = nursingProjectMapper.updateById(nursingProject);

        //2.删除缓存
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);

        //3.返回结果
        return result;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        //1.批量删除数据
        int result = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));

        //2.删除缓存
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);

        //3.返回结果
        return result;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        return nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        //1.从redis缓存中获取护理项目
        String nursingProjectList = redisTemplate.opsForValue().get(CacheConstants.NURSING_PROJECT_ALL_KEY);

        //2.判断缓存中是否存在
        if (StringUtils.isNotEmpty(nursingProjectList)) {
            return JSON.parseArray(nursingProjectList, NursingProjectVo.class);
        }

        //3.从数据库中获取
        List<NursingProjectVo> nursingProjectVoList = nursingProjectMapper.getAllProjects();

        //4.将数据写入缓存
        redisTemplate.opsForValue().set(CacheConstants.NURSING_PROJECT_ALL_KEY, JSON.toJSONString(nursingProjectVoList));

        //5.返回数据
        return nursingProjectVoList;
    }

    /**
     * 分页查询所有护理项目
     *
     * @param nursingProjectDto
     * @return
     */
    @Override
    public TableDataInfo<NursingProject> pageQueryNursingProject(NursingProjectDto nursingProjectDto) {

        Page<NursingProject> page = new Page<>(nursingProjectDto.getPageNum(), nursingProjectDto.getPageSize());

        page = page(page, Wrappers.<NursingProject>lambdaQuery()
                .like(StringUtils.isNotEmpty(nursingProjectDto.getName()), NursingProject::getName, nursingProjectDto.getName())
                .eq(nursingProjectDto.getStatus() != null, NursingProject::getStatus, nursingProjectDto.getStatus()));

        TableDataInfo<NursingProject> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());

        return tableDataInfo;
    }
}
